package com.example.finalproj;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.FlowLayout;

import javax.swing.JLabel;
import javax.swing.JButton;

import java.awt.Color;
import java.awt.Font;

import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.JTextField;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;

public class ChatServer_one extends JFrame implements Runnable{
	
	boolean server_flag=true;
	HashMap<String, ServerThread_one> user;

	private JPanel contentPane;
	private JPanel panel;
	private JLabel lblNewLabel;
	private JButton btn_sof;
	private JLabel lb_state;
	private JScrollPane scrollPane;
	JTextArea ta_chat;
	private JLabel lblNewLabel_2;
	private JPanel panel_1;
	private JTextField tf_msg;
	private JButton btn_send;
	private JLabel lblNewLabel_3;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args)
	{
		EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				try
				{
					ChatServer_one frame = new ChatServer_one();
					frame.setVisible(true);
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
	}
	
	public ChatServer_one() {
		setTitle("ChatServer_One");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		contentPane.add(getPanel(), BorderLayout.NORTH);
		contentPane.add(getScrollPane(), BorderLayout.CENTER);
		contentPane.add(getPanel_1(), BorderLayout.SOUTH);
		user = new HashMap<String, ServerThread_one>();
	}

	public JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getLblNewLabel(), BorderLayout.WEST);
			panel.add(getLb_state(), BorderLayout.CENTER);
			panel.add(getBtn_sof(), BorderLayout.EAST);
		}
		return panel;
	}
	public JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("\uC0C1\uD0DC : ");
		}
		return lblNewLabel;
	}
	public JButton getBtn_sof() {
		if (btn_sof == null) {
			btn_sof = new JButton("\uC11C\uBC84 \uC2DC\uC791");
			btn_sof.setForeground(Color.BLUE);
			btn_sof.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if(btn_sof.getText().equals("서버 시작"))
					{
						btn_sof.setText("서버 중지");
						btn_sof.setForeground(Color.red);
						lb_state.setText("정상");
						lb_state.setForeground(Color.blue);
						new Thread(ChatServer_one.this).start();
					}
					else
					{
						btn_sof.setText("서버 시작");
						btn_sof.setForeground(Color.blue);
						lb_state.setText("중지");
						lb_state.setForeground(Color.red);
						server_flag = false;
					}
				}
			});
		}
		return btn_sof;
	}
	public JLabel getLb_state() {
		if (lb_state == null) {
			lb_state = new JLabel("\uC911\uC9C0");
			lb_state.setForeground(Color.RED);
		}
		return lb_state;
	}
	public JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTa_chat());
			scrollPane.setColumnHeaderView(getLblNewLabel_2());
		}
		return scrollPane;
	}
	public JTextArea getTa_chat() {
		if (ta_chat == null) {
			ta_chat = new JTextArea();
		}
		return ta_chat;
	}
	public JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("\uB300\uD654\uB0B4\uC6A9");
			lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblNewLabel_2;
	}
	public JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.setLayout(new BorderLayout(0, 0));
			panel_1.add(getTf_msg(), BorderLayout.CENTER);
			panel_1.add(getBtn_send(), BorderLayout.EAST);
			panel_1.add(getLblNewLabel_3(), BorderLayout.WEST);
		}
		return panel_1;
	}
	public JTextField getTf_msg() {
		if (tf_msg == null) {
			tf_msg = new JTextField();
			tf_msg.setColumns(10);
		}
		return tf_msg;
	}
	public JButton getBtn_send() {
		if (btn_send == null) {
			btn_send = new JButton("\uC804\uC1A1");
		}
		return btn_send;
	}
	public JLabel getLblNewLabel_3() {
		if (lblNewLabel_3 == null) {
			lblNewLabel_3 = new JLabel("\uACF5\uC9C0");
		}
		return lblNewLabel_3;
	}

	public void run() {
		// TODO Auto-generated method stub
		try
		{
			ArrayList<ServerThread_one> al = new ArrayList<ServerThread_one>();
			InetAddress ia = InetAddress.getLocalHost();
			ta_chat.append(ia.getHostAddress()+"\n");
			ServerSocket server = new ServerSocket(8282);
			ta_chat.append("start\n");
			while(server_flag)
			{
				Socket client = server.accept();
				ta_chat.append("connect\n");
				al.add(new ServerThread_one(this, client));
			}
			ta_chat.append("stop\n");
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}

	
}
